﻿#region

using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

using Microsoft.Xna.Framework.Input;

#endregion

namespace Pob
{
    public class SettingsManager
    {
        #region Constants

        private const string SettingsFileName = @"settings.dat";

        #endregion

        #region Static Fields

        private static readonly SettingsManager Manager;

        #endregion

        #region Constructors and Destructors

        static SettingsManager()
        {
            Manager = new SettingsManager();

            Manager.Load();
        }

        private SettingsManager()
        {
        }

        #endregion

        #region Public Properties

        public static SettingsManager CurrentInstance
        {
            get
            {
                return Manager;
            }
        }

        public Settings Settings { get; private set; }

        #endregion

        #region Public Methods and Operators

        public bool Load()
        {
            Settings = new Settings();

            try
            {
                using (var fs = new FileStream(SettingsFileName, FileMode.Open))
                {
                    var binFormatter = new BinaryFormatter();
                    Settings = (Settings)binFormatter.Deserialize(fs);
                }
            }
            catch (Exception)
            {
                return false;
            }

            return true;
        }

        public void Save()
        {
            try
            {
                using (var fs = new FileStream(SettingsFileName, FileMode.Create))
                {
                    var binFormatter = new BinaryFormatter();
                    binFormatter.Serialize(fs, Settings);
                }
            }
            catch (Exception)
            {
            }
        }

        public void SetDefaultValues()
        {
            Settings.PlayerOne = new Player(null, "spieler1", Keys.W, Keys.S, Keys.Q, Side.Left);
            Settings.PlayerTwo = new Player(null, "spieler2", Keys.O, Keys.L, Keys.P, Side.Right);

            Settings.Height = 480;
            Settings.Width = 800;
            Settings.FpsCounter = false;
            Settings.PlayerCount = 1;
            Settings.PlayerThree = "PlayerThree";
            Settings.PlayerFour = "PlayerFour";
            Settings.StandardSpeed = 300;
        }

        #endregion
    }
}